package com.kasinf.sample.excel.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.RandomUtil;
import com.kasinf.framework.excel.core.define.ExcelResult;
import com.kasinf.framework.excel.core.util.ExcelUtils;
import com.kasinf.sample.excel.config.TestExcelListener;
import com.kasinf.sample.excel.entity.TestEntity;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : Kaiser
 * @Date: 2025/5/14  14:15
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    private final List<String> sexList = List.of("F", "M", "U");

    public List<TestEntity> data(int size) {
        // 构造一个静态列表。模拟查询数据库的数据
        List<TestEntity> list = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            TestEntity entity = new TestEntity();
            entity.setId(i);
            entity.setName("name" + i);
            entity.setAge(i);
            entity.setAddress("address" + i);
            entity.setSex(sexList.get(RandomUtil.randomInt(3)));
            entity.setType(RandomUtil.randomInt(2));
            list.add(entity);
        }
        return list;
    }

    @PostMapping("import")
    public String importExcel(MultipartFile file) throws IOException {
        List<TestEntity> read = ExcelUtils.read(file, TestEntity.class);
        read.forEach(System.out::println);
        return "success";
    }

    @PostMapping("import-validate")
    public ExcelResult<TestEntity> importCheckExcel(MultipartFile file) {
        try {
            return ExcelUtils.read(file.getInputStream(), TestEntity.class, true);
        } catch (Exception ignore) {

        }
        return null;
    }

    @PostMapping("import-listener")
    public ExcelResult<TestEntity> importListenerExcel(MultipartFile file) throws IOException {
        return ExcelUtils.read(file.getInputStream(), TestEntity.class, new TestExcelListener<>());
    }

    @GetMapping("export")
    public void exportExcel(HttpServletResponse response) {

        ExcelUtils.exportExcel(response, "测试数据", TestEntity.class, data(100));
    }

    @GetMapping("export-merge")
    public void exportExcel2(HttpServletResponse response) {
        ExcelUtils.exportExcel(response, "测试数据", TestEntity.class, data(100), true);
    }

    @GetMapping("export-template")
    public void exportExcel3(HttpServletResponse response) {
        ExcelUtils.exportTemplate(response, "template.xlsx", data(10));
    }

    @GetMapping("export-template2")
    public void exportExcel4(HttpServletResponse response) {
        Map<String, Object> data = new HashMap<>(2);
        data.put("list", data(10));

        Map<String, String> map = new HashMap<>(3);
        map.put("user", "超级管理员");
        map.put("date", DateTime.now().toDateStr());
        map.put("total", "10");

        data.put("data", map);

        ExcelUtils.exportTemplateMultiList(response, "template2.xlsx", data);
    }

    @GetMapping("export-template3")
    public void exportExcel5(HttpServletResponse response) {
        Map<String, Object> data = new HashMap<>(2);
        data.put("data", data(10));
        List<Map<String, Object>> datas = List.of(data, data);

        ExcelUtils.exportTemplateMultiSheet(response, "template3.xlsx", datas);
    }
}
